package com.hyphenate.easeui.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;

import com.hyphenate.EMError;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.R;
import com.hyphenate.easeui.manager.EaseVoiceRecorder;
import com.hyphenate.easeui.widget.chatrow.EaseChatRowVoicePlayer;

import java.util.Timer;
import java.util.TimerTask;


public class ChatVoiceActivity extends AppCompatActivity implements View.OnClickListener {
    ImageView ivChatVoice;
    Button btVoiceCancel;
    Button btVoiceOK;
    private EaseVoiceRecorder voiceRecorder;
    private Timer timer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_chat_voice);
        init();
    }

    private void init() {
        ivChatVoice = findViewById(R.id.iv_chatVoice);
        btVoiceCancel = findViewById(R.id.bt_voiceCancel);
        btVoiceCancel.setOnClickListener(this);
        btVoiceOK = findViewById(R.id.bt_voiceOK);
        btVoiceOK.setOnClickListener(this);
        Glide.with(this).asGif().load(R.drawable.chat_voice).into(ivChatVoice);
        voiceRecorder = new EaseVoiceRecorder(new Handler());
        startRecording();
    }

    public void startRecording() {
        EaseChatRowVoicePlayer voicePlayer = EaseChatRowVoicePlayer.getInstance(this);
        if (voicePlayer.isPlaying())
            voicePlayer.stop();
        try {
            voiceRecorder.startRecording(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        startVoiceTimer();
    }

    public void discardRecording() {
        try {
            // stop recording
            if (voiceRecorder.isRecording()) {
                voiceRecorder.discardRecording();
            }
        } catch (Exception e) {
        }
    }

    public int stopRecoding() {
        return voiceRecorder.stopRecoding();
    }

    private static final String TAG = "ChatVoiceActivity";
    public static final String VoicePath="VoicePath";
    public static final String VoiceLength="VoiceLength";
    public void finishRecording() {
        try {
            int length = stopRecoding();
            Log.i(TAG, "finishRecording: "+length);
            if (length > 0) {
                Log.i(TAG, "finishRecording: ");
                Intent intent=new Intent();
                intent.putExtra(VoicePath,getVoiceFilePath());
                intent.putExtra(VoiceLength,length);
                setResult(RESULT_OK,intent);
                finish();
//                if (recorderCallback != null) {
//                    recorderCallback.onVoiceRecordComplete(getVoiceFilePath(), length);
//                }
            } else if (length == EMError.FILE_INVALID) {
                Toast.makeText(this, com.hyphenate.easeui.R.string.Recording_without_permission, Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, com.hyphenate.easeui.R.string.The_recording_time_is_too_short, Toast.LENGTH_SHORT).show();
                finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, com.hyphenate.easeui.R.string.send_failure_please, Toast.LENGTH_SHORT).show();
        }

    }

    public String getVoiceFilePath() {
        return voiceRecorder.getVoiceFilePath();
    }


    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.bt_voiceCancel) {
            discardRecording();
            finish();
        } else if (view.getId() == R.id.bt_voiceOK) {
            finishRecording();
        }

    }

    /**
     * 设置最长录音时间60s
     */
    void startVoiceTimer(){
         timer=new Timer();
        TimerTask timerTask=new TimerTask() {
            @Override
            public void run() {
                finishRecording();
            }
        };

        timer.schedule(timerTask,60*1000);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        if(timer!=null)timer.cancel();
    }
}